<?php

namespace app\api\controller;

use app\api\business\UserBusiness;
use app\api\validate\UserValidate;
use app\BaseController;

class Login extends BaseController
{
    /**
     * 用户登录
     * 传递手机号、验证码、登录方式！
     */
    public function index()
    {
        if (request()->isPost()) {
            $input = [
                'phone_number' => input('post.phone_number'),
                'code' => input('post.code'),
                'type' => input('post.type')
            ];

            // 验证手机号
            $validate = new UserValidate();
            if (!$validate->scene('phone')->check($input)) {
                return show(config('status.error'), $validate->getError());
            }

            // 传入到business中处理逻辑
            $UserBusiness = new UserBusiness();
            $user = $UserBusiness->login($input);
            if ($user) {
                return show(config('status.success'), '登陆成功!', $user);
            }

            return show(config('status.error'), '登陆失败!');
        } else {
            return show(config('status.error'), '请求方式错误!');
        }
    }
}
